/* Copyright 2007-2008 dnAnalytics Project.
 *
 * Contributors to this file:
 * Patrick van der Velde
 *
 * This file is part of dnAnalytics.  dnAnalytics is licensed under the 
 * Microsoft Public License. See License.txt for a complete copy of the
 * license.
 */

using System;
using System.Diagnostics;
using dnAnalytics.LinearAlgebra.Solvers;
using dnAnalytics.LinearAlgebra;
using dnAnalytics.LinearAlgebra.Solvers.Preconditioners;
using dnAnalytics.Math;
using NUnit.Framework;
using System.Reflection;

namespace dnAnalytics.Tests.LinearAlgebra.Solvers.Preconditioners
{
    [TestFixture]
    [Category("Managed")]
    public sealed class DiagonalTest : PreconditionerTest
    {
        internal override IPreConditioner CreatePreconditioner()
        {
            return new Diagonal();
        }

        protected override void CheckResult(IPreConditioner preconditioner, SparseMatrix matrix, Vector vector, Vector result)
        {
            Assert.AreEqual(typeof(Diagonal), preconditioner.GetType(), "#01");

            Diagonal factorization = preconditioner as Diagonal;
            // Compute M * result = product
            // compare vector and product. Should be equal
            Vector product = new DenseVector(result.Count);
            matrix.Multiply(result, product);

            for (int i = 0; i < product.Count; i++)
            {
                Assert.IsTrue(Precision.EqualsWithinDecimalPlaces(vector[i],
                                                                  product[i],
                                                                  -Precision.Magnitude(s_Epsilon)),
                              "#02-" + i.ToString());
            }
        }
    }
}
